package com.elive.service.impl;

import com.elive.common.ServerResponse;
import com.elive.dao.ApartmentMapper;
import com.elive.dao.OrderMapper;
import com.elive.dao.UserMapper;
import com.elive.pojo.Apartment;
import com.elive.pojo.Order;
import com.elive.pojo.User;
import com.elive.service.IOrderService;
import com.elive.vo.OrderVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wuyin on 2018/4/16.
 */
@Service("iOrderService")
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ApartmentMapper apartmentMapper;

    @Override
    public ServerResponse<PageInfo> getOrderListByHostId(int pageNum, int pageSize, Integer hostId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderMapper.getOrderListByHostId(hostId);
        PageInfo pageResult = new PageInfo(orderList);
        if(orderList.size() != 0){
            List<OrderVo> orderVoList = new ArrayList<OrderVo>();
            for(int i = 0, length = orderList.size(); i < length; i++){
                Order order = orderList.get(i);
                Date startTime = order.getStartTime();
                Date endTime = order.getEndTime();
                String formatStartTime = DateFormat.getDateInstance().format(startTime);
                String formatEndTime = DateFormat.getDateInstance().format(endTime);

                Integer renderId = order.getRenderId();
                User user = userMapper.selectByPrimaryKey(renderId);

                Integer apartmentId = order.getApartmentId();
                Apartment apartment = apartmentMapper.selectByPrimaryKey(apartmentId);


                OrderVo orderVo = new OrderVo();
                orderVo.setId(order.getId());
                orderVo.setOrderNumber(order.getOrderNumber());
                orderVo.setApartmentId(order.getApartmentId());
                orderVo.setRenderId(order.getRenderId());
                orderVo.setHostId(order.getHostId());
                orderVo.setStartTime(formatStartTime);
                orderVo.setEndTime(formatEndTime);
                orderVo.setApartmentName(apartment.getApartmentName());
                orderVo.setRenderPhone(user.getPhone());
                orderVo.setRenderName(user.getUsername());
                orderVo.setRenderIdNumber(user.getIdNumber());
                orderVo.setRenderEmail(user.getEmail());
                orderVoList.add(orderVo);
            }
            PageInfo pageResult1 = new PageInfo(orderVoList);
            pageResult1.setTotal(pageResult.getTotal());
            pageResult1.setPageSize(pageResult.getPageSize());
            return ServerResponse.createBySuccess(pageResult1);
        }
        return ServerResponse.createByErrorMessage("没有订单信息");
    }

    @Override
    public ServerResponse<PageInfo> getOrderListByRenderId(int pageNum, int pageSize, Integer renderId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList =orderMapper.getOrderListByRenderId(renderId);
        PageInfo pageResult = new PageInfo(orderList);
        List<OrderVo> orderVoList = new ArrayList<OrderVo>();
        if(orderList.size() != 0){
            for(int i = 0, length = orderList.size(); i < length; i++){
                Order order = orderList.get(i);
                Date startTime = order.getStartTime();
                Date endTime = order.getEndTime();
                // 处理时间
                String formatStartTime = DateFormat.getDateInstance().format(startTime);
                String formatEndTime = DateFormat.getDateInstance().format(endTime);


                Integer apartmentId = order.getApartmentId();
                Apartment apartment = apartmentMapper.selectByPrimaryKey(apartmentId);



                OrderVo orderVo = new OrderVo();
                orderVo.setId(order.getId());
                orderVo.setOrderNumber(order.getOrderNumber());
                orderVo.setApartmentId(order.getApartmentId());
                orderVo.setRenderId(order.getRenderId());
                orderVo.setHostId(order.getHostId());
                orderVo.setStartTime(formatStartTime);
                orderVo.setEndTime(formatEndTime);
                orderVo.setApartmentName(apartment.getApartmentName());
                orderVoList.add(orderVo);
            }
            PageInfo pageResult1 = new PageInfo(orderVoList);
            pageResult1.setTotal(pageResult.getTotal());
            pageResult1.setPageSize(pageResult.getPageSize());
            pageResult1.setHasNextPage(pageResult.isHasNextPage());
            return ServerResponse.createBySuccess(pageResult1);
        }
        return ServerResponse.createByErrorMessage("没有订单信息");
    }

    @Override
    public ServerResponse<String> setOrder(Order order, String startTime, String endTime) {
        User user = userMapper.selectByPrimaryKey(order.getRenderId());
        String idNumber = user.getIdNumber();
        String email = user.getEmail();
        String phone = user.getPhone();
        if(StringUtils.isBlank(idNumber)){
            return ServerResponse.createByErrorMessage("请补充完整个人资料");
        }
        if(StringUtils.isBlank(email)){
            return ServerResponse.createByErrorMessage("请补充完整个人资料");
        }
        if(StringUtils.isBlank(phone)){
            return ServerResponse.createByErrorMessage("请补充完整个人资料");
        }


        int count = orderMapper.selectStartDate(order.getApartmentId(),startTime);
        if(count >= 1){
            return ServerResponse.createByErrorMessage("此段时间别墅已出租");
        }
        System.out.println(count+"_____________-------____");

        count = orderMapper.selectEndDate(order.getApartmentId(),endTime);
        if(count >= 1){
            return ServerResponse.createByErrorMessage("此段时间别墅已出租");
        }

        Date now=new Date();
        SimpleDateFormat formatNow = new SimpleDateFormat("yyyyMMddhhmmss");
        String formatNowStr = formatNow.format(now);
        System.out.println(formatNowStr);

        Date formatStartTime = null;
        Date formatEndTime = null;

        SimpleDateFormat formatSEDate=new SimpleDateFormat("yyyy-MM-dd");
        try {
            formatStartTime = formatSEDate.parse(startTime);
            formatEndTime = formatSEDate.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Order formatOrde = new Order();
        formatOrde.setRenderId(order.getRenderId());
        formatOrde.setHostId(order.getHostId());
        formatOrde.setApartmentId(order.getApartmentId());
        formatOrde.setOrderNumber(formatNowStr);
        formatOrde.setStartTime(formatStartTime);
        formatOrde.setEndTime(formatEndTime);

        int insertCount = orderMapper.insertSelective(formatOrde);
        if(insertCount > 0){
            return ServerResponse.createBySuccessMessage("下单成功");
        }
        return ServerResponse.createByErrorMessage("下单失败");
    }
}
